package com.web.cms.pan.service;

import com.blue.core.dict.OperateType;
import com.blue.core.dict.State;
import com.blue.core.id.IdGenerator;
import com.blue.core.util.RandomUtil;
import com.blue.jdbc.JdbcDataSource;
import com.blue.jdbc.Page;
import com.web.cms.pan.dao.OperatorLogDao;
import com.web.cms.pan.dao.ShareDao;
import com.web.cms.pan.dao.ShareLogDao;
import com.web.cms.pan.exception.ShareException;
import com.web.cms.pan.model.OperatorLog;
import com.web.cms.pan.model.Share;
import com.web.cms.pan.model.ShareLog;
import com.web.core.dict.PanExternalType;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 共享业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Service("panShareService")
@Transactional
@JdbcDataSource(Const.DS_CMS)
public class ShareService
{
	@Autowired
	private ShareDao shareDao;
	@Autowired
	private OperatorLogDao operatorLogDao;
	@Autowired
	private ShareLogDao shareLogDao;
	
	public ShareService()
	{
	}

	public void add(Share share)
	{
		share.setCode(IdGenerator.uuid16bit());
		if (share.getPassword() == null || share.getPassword().isEmpty())
			share.setPassword(RandomUtil.rand(RandomUtil.RandomType.UPPER_LOWER_CASE, 6));

		shareDao.save(share);
		operatorLogDao.add(share.getUserId(), "添加分享：" + share.getName(),
				share.getId(), PanExternalType.SHARE, OperateType.ADD);
	}

	public void update(Share share)
	{
		Share s = shareDao.get(share.getId());
		if (s == null || !s.getUserId().equals(share.getUserId()))
			throw new ShareException("共享不存在或不与用户关联：" + share.getId());

		shareDao.update(share);
		operatorLogDao.add(share.getUserId(), "修改分享：" + share.getName(),
				share.getId(), PanExternalType.SHARE, OperateType.UPDATE);
	}
	
	public Share get(Integer userId, Integer id)
	{
		Share share = shareDao.get(id);
		if (share == null || !userId.equals(share.getUserId()))
			throw new ShareException("共享不存或不与用户关联：" + id);

		return share;
	}

	public Share getByCode(Integer userId, String code)
	{
		Share share = shareDao.getObject("code", code, "state", State.NORMAL.getIndex());
		if (share != null && (share.getPassword() == null || share.getPassword().isEmpty()))
		{
			shareDao.inc(share.getId(), "count", 1);
			shareLogDao.add(userId, share.getId());
		}
		return share;
	}

	public Share valid(Integer userId, String code, String password)
	{
		Share share = shareDao.getObject("code", code, "password", password, "state", State.NORMAL.getIndex());
		if (share != null)
		{
			shareDao.inc(share.getId(), "count", 1);
			shareLogDao.add(userId, share.getId());
		}
		return share;
	}
	
	public Page<Share> list(Share share, Page<Share> page)
	{
		page = shareDao.list(share, page);
		return page;
	}
	
	public void delete(Integer userId, List<Integer> idList)
	{
		if (idList == null || idList.isEmpty())
			return;

		Date now = new Date();
		List<OperatorLog> logList = new ArrayList<>();
		for (Integer id : idList)
		{
			Share share = shareDao.get(id);
			if (share == null || !userId.equals(share.getUserId()))
				throw new ShareException("共享不存或不与用户关联：" + id);

			logList.add(operatorLogDao.create(userId, "删除分享：" + share.getName(), now,
					share.getId(), PanExternalType.SHARE, OperateType.DELETE));
		}
		shareLogDao.deleteByShare(idList);
		shareDao.delete(idList);
		operatorLogDao.saveList(logList);
	}

	public Page<ShareLog> listLog(ShareLog shareLog, Page<ShareLog> page)
	{
		page = shareLogDao.list(shareLog, page);
		return page;
	}
	
}
